package com.example.demo.controller;

import com.example.demo.service.AIService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/ai")
public class AIAssistantController {

    private final AIService aiService;

    @Autowired
    public AIAssistantController(AIService aiService) {
        this.aiService = aiService;
    }

    @PostMapping("/ask")
    public ResponseEntity<Map<String, Object>> askAI(@RequestBody Map<String, String> request) {
        String question = request.get("question");
        if (question == null || question.trim().isEmpty()) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("error", "问题不能为空");
            return ResponseEntity.badRequest().body(errorResponse);
        }

        String response = aiService.getAIResponse(question);
        
        Map<String, Object> result = new HashMap<>();
        result.put("response", response);
        return ResponseEntity.ok(result);
    }

    @PostMapping("/ask-multiple")
    public ResponseEntity<Map<String, Object>> askMultipleAI(
            @RequestBody Map<String, Object> request) {
        String question = (String) request.get("question");
        Integer count = request.containsKey("count") ? (Integer) request.get("count") : 2;
        
        if (question == null || question.trim().isEmpty()) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("error", "问题不能为空");
            return ResponseEntity.badRequest().body(errorResponse);
        }

        // 保证数量合理
        if (count < 1) count = 1;
        if (count > 5) count = 5;

        String[] responses = aiService.getMultipleAIResponses(question, count);
        
        Map<String, Object> result = new HashMap<>();
        result.put("responses", responses);
        return ResponseEntity.ok(result);
    }
} 